package com.gjr.eduservice.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.gjr.commonutils.Result;
import com.gjr.eduservice.entity.EduCourse;
import com.gjr.eduservice.entity.vo.CourseInfoVo;
import com.gjr.eduservice.entity.vo.CoursePublishVo;
import com.gjr.eduservice.entity.vo.EduCourseVo;
import com.gjr.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程管理 前端控制器
 * </p>
 *
 * @author gjr
 * @since 2022-09-01
 */
@Api(description = "课程管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 课程列表
     *
     * @param eduCourseVo 分页和条件查询对象
     * @return 结果
     */
    @PostMapping("/getCourseList")
    public Result<?> getCourseList(@RequestBody EduCourseVo eduCourseVo) {
        HashMap<String, Object> map = eduCourseService.getCourseList(eduCourseVo);
        return Result.ok().data("map", map);
    }

    /**
     * 添加课程基本信息
     *
     * @param courseInfoVo 课程对象
     * @return json
     */
    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("/addCourseInfo")
    public Result<?> addCourse(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = eduCourseService.addCourseInfo(courseInfoVo);

        return Result.ok().data("courseId", courseId);
    }

    /**
     * 根据课程Id 查询课程基本信息
     *
     * @param courseId 课程Id
     * @return 课程信息
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public Result<?> getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfoVo", courseInfoVo);
    }

    /**
     * 修改课程信息
     *
     * @param courseInfoVo 课程信息对象
     * @return 成功与否
     */
    @PostMapping("/updateCourseInfo")
    public Result<?> updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    /**
     * 根据课程id查询课程确认信息
     *
     * @param courseId 课程id
     * @return 课程确认信息
     */
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public Result<?> getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(courseId);
        return Result.ok().data("coursePublishVo", coursePublishVo);
    }

    /**
     * 课程最终发布
     *
     * @param id 课程id
     * @return 返回值
     */
    @PostMapping("publishCourse/{id}")
    public Result<?> publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        eduCourseService.updateById(eduCourse);
        return Result.ok();
    }

    /**
     * 删除课程
     * 小节、章节、描述、课程本身都需要删除
     *
     * @param courseId 课程id
     * @return 返回值
     */
    @DeleteMapping("/deleteCourse/{courseId}")
    public Result<?> deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return Result.ok();
    }
}

